package com.main.modulemysql.web;

import com.alibaba.fastjson.JSON;
import com.main.modulemysql.entity.ChartRateVO;
import com.main.modulemysql.entity.EquipmentsCategory;
import com.main.modulemysql.entity.TableRateVO;
import com.main.modulemysql.service.IChartIndexService;
import com.main.modulemysql.service.IEquipmentService;
import com.main.modulemysql.service.IEquipmentsCategoryService;
import com.main.modulemysql.service.IHjzkFaultFindService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: IceZhang
 * @Date: 2018/8/6 16:57
 * @Description:
 */
@Api(tags = "Chart Controller about statistics DashBoard")
@RestController
@ResponseBody
public class ChartIndexController {

    @Autowired
    IEquipmentService iEquipmentService;

    @Autowired
    IEquipmentsCategoryService iEquipmentsCategoryService;

    @Autowired
    IHjzkFaultFindService iHjzkFaultFindService;
    @Autowired
    IChartIndexService iChartIndexService;

    @ApiOperation(value = "获取隧道统计仪表盘设施指标、预案执行指标、交通指标、环境指标等数据",notes = "获取隧道统计仪表盘设施指标、预案执行指标、交通指标、环境指标等数据")
    @RequestMapping(value = "/dashBoardData",method = RequestMethod.POST)
    @ResponseBody
    public String statisticsDashBoard(@ApiParam(name = "设施code") String facilityCode){
        HashMap<String, Object> resultMap = new HashMap<>();

        HashMap<String, Object> facilitiesTarget = new HashMap<>();
        Integer allEquipCount = iEquipmentService.getEquipmentCountByFacilityCode(facilityCode);
        Integer allFaultCount = iEquipmentService.getFaultEquipmentCountByFacilityCode(facilityCode);
        facilitiesTarget.put("faultCount",allFaultCount);
        facilitiesTarget.put("equipCount",allEquipCount);

        ArrayList<Map<String, String>> mapsList = new ArrayList<>();

        List<EquipmentsCategory> equipmentsCategories = iEquipmentsCategoryService.selectCategoryInfo();
        for (EquipmentsCategory cateogory:equipmentsCategories) {
            Integer abnormalCount = iHjzkFaultFindService.selectFaultCountByCategoryCode(cateogory.getCode());
            Integer equipCount = iEquipmentService.getEquipmentCountByCateogoryCode(cateogory.getCode());
            if(abnormalCount>0&&equipCount>0){
                HashMap<String, String> detailMapItem = new HashMap<>();
                detailMapItem.put("categoryName",cateogory.getName());
                detailMapItem.put("equipCount",equipCount+"");
                detailMapItem.put("abnormalCount",abnormalCount+"");
                //detailMapItem.put("nomalRate",abnormalCount/equipCount*100+"");
                mapsList.add(detailMapItem);
            }
        }
        facilitiesTarget.put("mapsList",mapsList);
        resultMap.put("facilitiesTarget",facilitiesTarget);
        resultMap.put("detectTest",facilitiesTarget);
        String s = JSON.toJSONString(resultMap);
        return ""+s;

    }

    @ApiOperation(value = "获取单个设施的完好率及故障设备详情")
    @RequestMapping(value = "/singTargetAndDetails",method = RequestMethod.POST)
    @ResponseBody
    public String singTargetAndDetails(@ApiParam(name = "设施code") String facilityCode){
        Map<String, Object> facilityTarget = iChartIndexService.getFacilityTarget(facilityCode);
        return JSON.toJSONString(facilityTarget);
    }

    @ApiOperation(value = "获取单个设施的完好率")
    @RequestMapping(value = "/singSingleTarget",method = RequestMethod.POST)
    @ResponseBody
    public String singleTargetRate(@ApiParam(name = "设施code") String facilityCode){
        Map<String, Object> singleTarget = iChartIndexService.getSingleTarget(facilityCode);
        return JSON.toJSONString(singleTarget);
    }

    @ApiOperation(value = "获取完好率报表的数据")
    @RequestMapping(value = "/chartRateList" ,method = RequestMethod.POST)
    @ResponseBody
    public String chartRateList(@RequestParam(value = "month",defaultValue = "1")@ApiParam(name = "过去几个月(整数)") Integer month, String facilityCode){

        List<ChartRateVO> chartRateVO = iHjzkFaultFindService.getChartRateVO(month, facilityCode);
        Integer equipCount = iEquipmentService.getEquipmentCountByFacilityCode(facilityCode);
        List<Map<String,String>>list=new ArrayList<>();
        HashMap<String, Object> resultMap = new HashMap<>();
        for (ChartRateVO item:chartRateVO) {
            HashMap<String, String> itemMap = new HashMap<>();
            if(item.getCount()==0){
                itemMap.put("rate",0+"");
            }else{
                DecimalFormat df=new DecimalFormat("0.00");
                String result = df.format(1-(float) item.getCount() / equipCount * 100);
                itemMap.put("rate",result+"%");
            }
            itemMap.put("day",item.getDay());
            list.add(itemMap);
        }
        return JSON.toJSONString(list);

    }

    @ApiOperation(value = "获取完好率表格数据")
    @RequestMapping(value = "/tableRateList",method = RequestMethod.POST)
    @ResponseBody
    public String tableRateList(@RequestParam(value = "month",defaultValue = "1")@ApiParam(name = "过去几个月(整数)") Integer month){
        List<TableRateVO> tableRateVO = iHjzkFaultFindService.getTableRateVO(month);
        return JSON.toJSONString(tableRateVO);
    }



}